package org.example;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;

public class AddUserDialog extends JDialog {
    private final UserManagementFrame parentFrame;
    private JTextField idField;
    private JTextField nameField;
    private JPasswordField passwordField;
    private JPasswordField confirmPasswordField;
    private JComboBox<String> roleCombo;

    public AddUserDialog(UserManagementFrame parent) {
        super(parent, "添加新用户", true);
        this.parentFrame = parent;
        setSize(400, 350);
        setLocationRelativeTo(parent);
        setLayout(null);

        // 用户ID
        JLabel idLabel = new JLabel("用户ID*:");
        idLabel.setBounds(30, 30, 100, 25);
        add(idLabel);

        idField = new JTextField();
        idField.setBounds(130, 30, 220, 25);
        add(idField);

        // 姓名
        JLabel nameLabel = new JLabel("姓名*:");
        nameLabel.setBounds(30, 70, 100, 25);
        add(nameLabel);

        nameField = new JTextField();
        nameField.setBounds(130, 70, 220, 25);
        add(nameField);

        // 密码
        JLabel passLabel = new JLabel("密码*:");
        passLabel.setBounds(30, 110, 100, 25);
        add(passLabel);

        passwordField = new JPasswordField();
        passwordField.setBounds(130, 110, 220, 25);
        add(passwordField);

        // 确认密码
        JLabel confirmPassLabel = new JLabel("确认密码*:");
        confirmPassLabel.setBounds(30, 150, 100, 25);
        add(confirmPassLabel);

        confirmPasswordField = new JPasswordField();
        confirmPasswordField.setBounds(130, 150, 220, 25);
        add(confirmPasswordField);

        // 角色
        JLabel roleLabel = new JLabel("角色*:");
        roleLabel.setBounds(30, 190, 100, 25);
        add(roleLabel);

        roleCombo = new JComboBox<>(new String[]{"student", "teacher", "admin"});
        roleCombo.setBounds(130, 190, 220, 25);
        add(roleCombo);

        // 按钮
        JButton saveButton = new JButton("保存");
        saveButton.setBounds(100, 250, 100, 30);
        saveButton.addActionListener(this::saveUser);
        add(saveButton);

        JButton cancelButton = new JButton("取消");
        cancelButton.setBounds(220, 250, 100, 30);
        cancelButton.addActionListener(e -> dispose());
        add(cancelButton);
    }

    private void saveUser(ActionEvent e) {
        String id = idField.getText().trim();
        String name = nameField.getText().trim();
        String password = new String(passwordField.getPassword());
        String confirmPassword = new String(confirmPasswordField.getPassword());
        String role = (String) roleCombo.getSelectedItem();

        // 验证输入
        if (id.isEmpty() || name.isEmpty() || password.isEmpty()) {
            JOptionPane.showMessageDialog(this, "带*号的字段必须填写", "输入错误", JOptionPane.ERROR_MESSAGE);
            return;
        }

        if (!password.equals(confirmPassword)) {
            JOptionPane.showMessageDialog(this, "两次输入的密码不一致", "输入错误", JOptionPane.ERROR_MESSAGE);
            return;
        }

        if (UserDAO.isUserIdExists(id)) {
            JOptionPane.showMessageDialog(this, "用户ID已存在", "添加失败", JOptionPane.ERROR_MESSAGE);
            return;
        }

        // 添加用户
        String hashedPassword = PasswordUtil.hashPassword(password);
        User newUser = new User(id, name, hashedPassword, role);

        if (UserDAO.addUser(newUser)) {
            JOptionPane.showMessageDialog(this, "用户添加成功", "成功", JOptionPane.INFORMATION_MESSAGE);
            parentFrame.refreshTable(); // 刷新父窗口的表格
            dispose();
        } else {
            JOptionPane.showMessageDialog(this, "用户添加失败", "失败", JOptionPane.ERROR_MESSAGE);
        }
    }
}